'''
多窗口切换的常用方法：switch_to.window()
(1)current_window_handle		获取当前窗口句柄
(2)window_handles			获取所有窗口句柄
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from time import sleep
chrome_location = r'D:\chrom browser\chrome-win\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
dr = webdriver.Chrome(options=options)
dr.get(r'https://www.baidu.com/')
sleep(3)
print('当前句柄是：')
print(dr.current_window_handle)

dr.find_element(By.LINK_TEXT,'hao123').click()
sleep(3)
print('当前窗口的句柄是：')
print(dr.current_window_handle)

baidu_handle = dr.window_handles[0]
hao123_handle=dr.window_handles[1]
#切换到baidu
dr.switch_to.window(baidu_handle)
sleep(2)
dr.switch_to.window(hao123_handle)
sleep(2)
print(dr.window_handles)
dr.quit()